package com.railway.service.impl;

import java.util.List;

import com.railway.bean.Contact;
import com.railway.bean.Expenditures;
import com.railway.bean.Integra;
import com.railway.bean.LoseCreditUser;
import com.railway.bean.Page;
import com.railway.bean.Revenue;
import com.railway.bean.User;
import com.railway.dao.ContactDao;
import com.railway.dao.IntegraDao;
import com.railway.dao.UserDao;
import com.railway.dao.impl.ContactDaoImpl;
import com.railway.dao.impl.IntegraDaoImpl;
import com.railway.dao.impl.UserDaoImpl;
import com.railway.service.UserService;

public class UserServiceImpl implements UserService {
	
	UserDao ud = new UserDaoImpl();
	ContactDao cd = new ContactDaoImpl();
	IntegraDao itd = new IntegraDaoImpl();
	
	@Override
	public User login(String username, String password) {
		User user = ud.selectUser(username,password);
		int isdelete = ud.selectIsDelete(username);
		if(user != null && isdelete == 0) {
			return user;
		}
		return null;
	}
	@Override
	public int deleteUser(int user_id) {
		return ud.deleteUser(user_id);
	}
	@Override
	public int regester(User user) {
		if(ud.selectUserByName(user.getUser_name())!= 1) {
			if(ud.selectUserByEmail(user.getUser_email()) != 1) {
				if(ud.selectUserByPhoneNum(user.getUser_phone()) != 1) {
					int temp = ud.insertUser(user);
					if(temp == 1) {
						int user_id = ud.selectUserByName(user.getUser_name());
						Contact c = new Contact(user_id, user.getUser_name(), user.getUser_phone(),
								user.getUser_id_type(), user.getUser_id_num(), user.getPassenger_type());
						addContact(c);
						cd.insertContact(1, c);
						itd.insertIntegra(user_id);
						return temp;
					}
				}else {
					return -3;
				}
			}else {
				return -2;
			}
		}else {
			return -1;
		}
		
		return 0;
	}
	@Override
	public int addContact(Contact contact) {
		return cd.insertContact(cd.selectContactNum(contact.getUser_id())+1,contact);
	}
	
	@Override
	public int deleteContact(int user_id, int contact_id) {
		return cd.deleteContact(user_id, contact_id);
	}

	@Override
	public List<Contact> selectContact(int user_id) {
		return cd.showContactByUser(user_id);
	}


	@Override
	public Page<User> selectUserAll(int pageNum, int pageSize) {
		List<User> data = ud.selectAllUser(pageNum, pageSize);
		int totalRows = ud.selectCountUser();
		Page<User> page = new Page(data,pageNum,pageSize,totalRows);
		return page;
	}


	@Override
	public List<Contact> selectContactByIdAndName(int user_id, String user_name) {
		return cd.selectContactByIdAndName(user_id, user_name);
	}
	@Override
	public int addUser(User u) {
		// TODO Auto-generated method stub
		return ud.addUser(u);
	}
	@Override
	public User selectByUserName(String userName) {
		
		return ud.selectByUserName(userName);
	}
	@Override
	public int updatePhone(String phoneNum, String userName, String userPassword) {
		return ud.updatePhone(phoneNum, userName, userPassword);
	}
	@Override
	public int updateEmail(String emailNew, String userName, String password) {
		return ud.updateEmail(emailNew, userName, password);
	}
	@Override
	public int updatePassword(String passwordNew, String userName, String password) {
		
		return ud.updatePassword(passwordNew, userName, password);
	}
	@Override
	public int updateUserById(int user_id, String user_name, String user_phone, String user_password, String user_email,
			String consumption_password) {
		// TODO Auto-generated method stub
		return ud.updateUserById(user_id, user_name, user_phone, user_password, user_email, consumption_password);
	}
}
